package com.intelligence.tio.server.handler;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.OnlineUserListReqBody;
import com.intelligence.tio.common.packages.OnlineUserListRespBody;
import com.intelligence.tio.common.packages.P2PReqBody;
import com.intelligence.tio.common.packages.P2PRespBody;
import com.intelligence.tio.common.packages.UserToOnlineRespBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.ObjWithLock;
import org.tio.json.Json;

/**
 * 
* @Title: P2PReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 在线用户列表请求消息处理器
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:47:25 
* @version V1.0
 */
public class UserToOnlineReqHandler extends AbsTioBsHandler<OnlineUserListReqBody>
{
	private static Logger log = LoggerFactory.getLogger(UserToOnlineReqHandler.class);

	@Override
	public Class<OnlineUserListReqBody> bodyClass()
	{
		return OnlineUserListReqBody.class;
	}

	@Override
	public Object handler(JsonPacket packet, OnlineUserListReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到在线用户请求推送给其他用户消息消息:{}", Json.toJson(bsBody));
		
		//获取请求用户ID
		String userId = channelContext.getSessionContext().getUserId();
		String userName = channelContext.getSessionContext().getUserName();
		String userPhone = channelContext.getSessionContext().getUserPhone();
		Integer roleId = channelContext.getSessionContext().getRoleId();
		String groupId = channelContext.getSessionContext().getGroupId();
		 //群发消息通知用户上线
		JsonPacket userToOnlinePacket = new JsonPacket();
		UserToOnlineRespBody userBody = new UserToOnlineRespBody();
		userBody.setUserId(userId);
		userBody.setUserName(userName);
		userBody.setUserPhone(userPhone);
		userBody.setRoleId(roleId);
		userBody.setCommand(Command.COMMAND_USERTOONLINE_RESP.getValue());
		userToOnlinePacket.setBody(Json.toJson(userBody).getBytes(JsonPacket.CHARSET));
		Aio.sendToGroup(channelContext.getGroupContext(), groupId, userToOnlinePacket);
		return null;
	}
}
